import Koa from 'koa'
import { koaBody } from 'koa-body'
import Router from '@koa/router'
import Database from 'better-sqlite3'
import { formatDate } from '@n2/shared-utils'

// 初始化数据库连接
const db = new Database('database.sqlite', { verbose: console.log })

// 创建 Koa 应用实例
const app = new Koa()
const router = new Router()

// 配置中间件
app.use(koaBody())

// 基础路由
router.get('/', (ctx) => {
  ctx.body = {
    message: 'Welcome to N2 Server',
    currentTime: formatDate(new Date())
  }
})

// 注册路由
app.use(router.routes()).use(router.allowedMethods())

// 启动服务器
const port = 3000
app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`)
})